﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.Recycling.Behavior;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.Recycling.Object;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;
using Imaginecup.recycling.Object.Entity;
using Imaginecup.recycling.Object;

namespace Imaginecup.recycling.Behavior
{
    public class AnimationBehavior : IAnimationBehavior
    {
        private int m_FrameNumber = 0;
        private double m_CurrentTime = 0;
        private double m_DelayTime = 0;
        private bool m_bLoopAnimation = true;         //애니메이션할 것인지 확인
        private bool m_bFinishFlag = false;
        private Texture2D m_Texture;
        private int frameWidth;
        private int frameHeight;
        private CharacterState m_Current;
        private CharacterState m_Next;

        #region Properties
        public double Delay
        {
            set { m_DelayTime = value; }
        }
        public CharacterState CurrentCharacterState
        {
            get { return m_Current; }
            set { m_Current = value;}
        }
        public CharacterState NextCharacterState
        {
            get { return m_Next; }
            set { m_Next = value; }
        }
        public int EndFrame
        {
            get { return m_Texture.Width / frameWidth; }
        }
        public Rectangle FrameRectangle
        {
            get
            {
                return new Rectangle(
                    m_FrameNumber * frameWidth,
                    0,
                    frameWidth,
                    frameHeight);
            }
        }
        #endregion

        public AnimationBehavior() { }

        public AnimationBehavior(Texture2D texture, int frameWidth)
        {
            this.m_Texture = texture;
            this.frameWidth = frameWidth;
            this.frameHeight = texture.Height;
        }

        public void UpdateAnimation(GameTime gameTime)
        {
            m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;

            if (m_CurrentTime >= m_DelayTime)
            {
                m_CurrentTime = m_CurrentTime - m_DelayTime;

                if (++m_FrameNumber >= EndFrame)
                {
                    if (m_bLoopAnimation) //무한 루프면 반복
                    {
                        m_FrameNumber = 0;
                    }
                    else                //무한 루프가 아니면 Finish상태를 true로 변경
                    {
                        m_FrameNumber = EndFrame - 1;
                        m_bFinishFlag = true;
                    }
                }
            }
        }


        public int GetFramNumber()
        {
            return m_FrameNumber;
        }
        public Texture2D GetTexture()
        {
            return m_Texture;
        }
        public void SetLoopAnimationFlag(bool flag)
        {
            m_bLoopAnimation = flag;
        }
        public bool GetFinishFlag()
        {
            return m_bFinishFlag;
        }
        public CharacterState GetNextAnimationKey()
        {
            return m_Next;
        }

        public void ResetAnimation()
        {
            m_FrameNumber = 0;
            m_bFinishFlag = false;
        }

        public void SetTexture(Texture2D texture)
        {
            m_Texture = texture;
        }
        public void Update(GameTime gameTime)
        {
            UpdateAnimation(gameTime);
        }
    }
}


